﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shop.Bll.trade
{
    public class XbotHelper
    {
        public string GetOutStockPara()
        {
            // 旺店通列字段
          //string wdtText = "['', '订单编号', '原始单号', '子单原始单号', '原始子订单号', '订单类型', '支付账号', '出库单编号', '仓库', '仓库类型', '店铺', '出库单状态', '出库状态', '分拣序号', '商家编码', '货品编号', '货品名称', '货品简称', '品牌', '分类', '规格码', '规格名称', '条形码', '货品数量', '货品原单价', '货品原总金额', '订单总优惠', '邮费', '货品成交价', '货品成交总价', '货品总优惠', '货到付款金额', '货品成本', '货品总成本', '订单支付金额', '应收金额', '退款前支付金额', '单品支付金额', '分摊邮费', '预估邮资', '邮资成本', '订单包装成本', '订单毛利', '毛利率', '客户网名', '收件人', '证件号码', '收货地区', '收货地址', '收件人手机', '收件人电话', '物流公司', '实际重量', '预估重量', '需开发票', '制单人', '打单员', '拣货员', '打包员', '检视员', '业务员', '验货员', '打印波次', '物流单打印状态', '发货单打印状态', '分拣单打印状态', '物流单号', '分拣单编号', '外部单号', '付款时间', '发货时间', '赠品方式', '买家留言', '客服备注', '打印备注', '备注', '包装', '来源组合装编码', '拆自组合装', '来源组合装数量', '体积', '分销商', '分销商编号', '下单时间', '审核时间']";
            string wdtText = "['', '订单编号', '原始单号', '子单原始单号', '原始子订单号', '订单类型', '支付账号', '出库单编号', '仓库', '仓库类型', '店铺', '出库单状态', '出库状态', '分拣序号', '商家编码', '货品编号', '货品名称', '货品简称', '品牌', '分类', '规格码', '规格名称', '条形码', '货品数量', '货品原单价', '货品原总金额', '订单总优惠', '邮费', '货品成交价', '货品成交总价', '货品总优惠', '货到付款金额', '货品成本', '货品总成本', '订单支付金额', '应收金额', '退款前支付金额', '单品支付金额', '分摊邮费', '预估邮资', '邮资成本', '订单包装成本', '订单毛利', '毛利率', '客户网名', '收件人', '证件号码', '收货地区', '收货地址', '收件人手机', '收件人电话', '物流公司', '实际重量', '预估重量', '需开发票', '制单人', '打单员', '拣货员', '打包员', '检视员', '业务员', '验货员', '打印波次', '物流单打印状态', '发货单打印状态', '分拣单打印状态', '物流单号', '分拣单编号', '外部单号', '付款时间', '发货时间', '赠品方式', '买家留言', '客服备注', '打印备注', '备注', '包装', '来源组合装编码', '拆自组合装', '来源组合装数量', '体积', '分销商', '分销商编号', '下单时间', '审核时间']";

            //sql 插入字段
            var sqlFields = new string[]
            {
                "logistics_no",
"goodsAmount",
"warehouse",
"spec_no",
"trade_no",
"src_tids",
"cs_remark",
"src_tid",
"src_oid",
"trade_type",
"pay_account",
"outstock_no",
"warehouse_type",
"printer",
"shop_name",
"status",
"consign_status",
"picklist_seq",
"goods_no",
"goods_name",
"goods_shot_name",
"brand_name",
"goods_group",
"spec_code",
"spec_name",
"barcode",
"num",
"goods_original_price",
"goods_original_amount",
"order_amount",
"post_amount",
"sell_price",
"sell_amount",
"discount",
"cod_amount",
"cost_price",
"cost_amount",
"paid",
"receivable",
"paid_before_return",
"share_post",
"calc_post_cost",
"post_fee",
"package_cost",
"profit",
"profit_rate",
"customer_nick_name",
"receiver_name",
"id_card",
"receiver_position",
"receiver_address",
"receiver_phone",
"receiver_phone2",
"logistics_name",
"weight",
"calc_weight",
"invoicing_required",
"operator_name",
"picker_name",
"packager_name",
"checker",
"salesman_name",
"examiner_name",
"print_no",
"logistics_print_status",
"sendbill_print_status",
"picklist_print_status",
"picklist_no",
"outer_no",
"pay_time",
"consign_time",
"gift_methods",
"buyer_message",
"print_remark",
"remark",
"is_package",
"suite_no",
"self_assembly",
"from_assembly_qty",
"volumetric",
"fenxiao_nick",
"trade_time",
"stock_check_time",
"fenxiao_no",
"rece_province",
"rece_city",
"rece_county",
            };

            // 顺序
            var order = new Dictionary<string, string>
            {
                { "logistics_no","物流单号"},
{ "goodsAmount","单品支付金额"},
{ "warehouse","仓库"},
{ "spec_no","商家编码"},
{ "trade_no","订单编号"},
{ "src_tids","原始单号"},
{ "cs_remark","客服备注"},
{ "src_tid","子单原始单号"},
{ "src_oid","原始子订单号"},
{ "trade_type","订单类型"},
{ "pay_account","支付账号"},
{ "outstock_no","出库单编号"},
{ "warehouse_type","仓库类型"},
{ "printer","打单员"},
{ "shop_name","店铺"},
{ "status","出库单状态"},
{ "consign_status","出库状态"},
{ "picklist_seq","分拣序号"},
{ "goods_no","货品编号"},
{ "goods_name","货品名称"},
{ "goods_shot_name","货品简称"},
{ "brand_name","品牌"},
{ "goods_group","分类"},
{ "spec_code","规格码"},
{ "spec_name","规格名称"},
{ "barcode","条形码"},
{ "num","货品数量"},
{ "goods_original_price","货品原单价"},
{ "goods_original_amount","货品原总金额"},
{ "order_amount","订单总优惠"},
{ "post_amount","邮费"},
{ "sell_price","货品成交价"},
{ "sell_amount","货品成交总价"},
{ "discount","货品总优惠"},
{ "cod_amount","货到付款金额"},
{ "cost_price","货品成本"},
{ "cost_amount","货品总成本"},
{ "paid","订单支付金额"},
{ "receivable","应收金额"},
{ "paid_before_return","退款前支付金额"},
{ "share_post","分摊邮费"},
{ "calc_post_cost","预估邮资"},
{ "post_fee","邮资成本"},
{ "package_cost","订单包装成本"},
{ "profit","订单毛利"},
{ "profit_rate","毛利率"},
{ "customer_nick_name","客户网名"},
{ "receiver_name","收件人"},
{ "id_card","证件号码"},
{ "receiver_position","收货地区"},
{ "receiver_address","收货地址"},
{ "receiver_phone","收件人手机"},
{ "receiver_phone2","收件人电话"},
{ "logistics_name","物流公司"},
{ "weight","实际重量"},
{ "calc_weight","预估重量"},
{ "invoicing_required","需开发票"},
{ "operator_name","制单人"},
{ "picker_name","拣货员"},
{ "packager_name","打包员"},
{ "checker","检视员"},
{ "salesman_name","业务员"},
{ "examiner_name","验货员"},
{ "print_no","打印波次"},
{ "logistics_print_status","物流单打印状态"},
{ "sendbill_print_status","发货单打印状态"},
{ "picklist_print_status","分拣单打印状态"},
{ "picklist_no","分拣单编号"},
{ "outer_no","外部单号"},
{ "pay_time","付款时间"},
{ "consign_time","发货时间"},
{ "gift_methods","赠品方式"},
{ "buyer_message","买家留言"},
{ "print_remark","打印备注"},
{ "remark","备注"},
{ "is_package","包装"},
{ "suite_no","来源组合装编码"},
{ "self_assembly","拆自组合装"},
{ "from_assembly_qty","来源组合装数量"},
{ "volumetric","体积"},
{ "fenxiao_nick","分销商"},
{ "trade_time","下单时间"},
{ "stock_check_time","审核时间"},
{ "fenxiao_no","分销商编号"},

            };

            int i = 0;


            var wdtArr2 = wdtText.Trim('[', ']')
            .Replace("'", string.Empty)
            .Replace(" ", string.Empty)
            .Split(',')
            .Select(a =>
            {
                var find = order.Where(c => c.Value == a).FirstOrDefault();
                if(a== "物流单号")
                {

                }
                if (string.IsNullOrEmpty(find.Key) == false)
                {
                    var ind = Array.IndexOf(sqlFields, find.Key);
                    //第一个元素舍弃， 这里减1
                    return new { ind = ind, val = i++, name = a, field = find.Key };  //ind 是sql参数的顺序, val是旺店通的
                }
                else
                {
                    return new { ind = -1, val = -1, name = string.Empty, field = string.Empty };
                }
            })
            .Where(a => a.ind >= 0)
            .OrderBy(a => a.ind)
            .ToList();

            foreach (var item in wdtArr2)
            {
                Console.WriteLine(item);
            }
            //var nof = sqlFields.Where(a => wdtArr2.Any(c => c.field == a) == false)
            //    .ToList();

            var wdtArr= wdtArr2.Select(a => a.val)
            .ToArray();


            var res = $@"data.append(( a[{wdtArr[0]}], str2Num(a[{wdtArr[1]}]), a[{wdtArr[2]}], a[{wdtArr[3]}], a[{wdtArr[4]}].replace('\n', '').replace('\r', '').strip(), a[{wdtArr[5]}], a[{wdtArr[6]}], a[{wdtArr[7]}], a[{wdtArr[8]}], a[{wdtArr[9]}], a[{wdtArr[10]}], a[{wdtArr[11]}], a[{wdtArr[12]}],
             a[{wdtArr[13]}], a[{wdtArr[14]}], a[{wdtArr[15]}], a[{wdtArr[16]}], str2Num(a[{wdtArr[17]}]), a[{wdtArr[18]}], a[{wdtArr[19]}], a[{wdtArr[20]}], a[{wdtArr[21]}], a[{wdtArr[22]}], a[{wdtArr[23]}], a[{wdtArr[24]}], a[{wdtArr[25]}],
              str2Num(a[{wdtArr[26]}]), str2Num(a[{wdtArr[27]}]), str2Num(a[{wdtArr[28]}]), str2Num(a[{wdtArr[29]}]), str2Num(a[{wdtArr[30]}]), str2Num(a[{wdtArr[31]}]), str2Num(a[{wdtArr[32]}]), 
              str2Num(a[{wdtArr[33]}]), str2Num(a[{wdtArr[34]}]), str2Num(a[{wdtArr[35]}]), str2Num(a[{wdtArr[36]}]), str2Num(a[{wdtArr[37]}]), str2Num(a[{wdtArr[38]}]), a[{wdtArr[39]}], 
              str2Num(a[{wdtArr[40]}]), str2Num(a[{wdtArr[41]}]), str2Num(a[{wdtArr[42]}]), str2Num(a[{wdtArr[43]}]), str2Num(a[{wdtArr[44]}]), str2Num(a[{wdtArr[45]}]), a[{wdtArr[46]}], a[{wdtArr[47]}],
              a[{wdtArr[48]}], a[{wdtArr[49]}], a[{wdtArr[50]}], a[{wdtArr[51]}], a[{wdtArr[52]}], a[{wdtArr[53]}], str2Num(a[{wdtArr[54]}]), str2Num(a[{wdtArr[55]}]), a[{wdtArr[56]}], a[{wdtArr[57]}], a[{wdtArr[58]}], a[{wdtArr[59]}],
              a[{wdtArr[60]}], a[{wdtArr[61]}], a[{wdtArr[62]}], a[{wdtArr[63]}], a[{wdtArr[64]}], a[{wdtArr[65]}], a[{wdtArr[66]}], a[{wdtArr[67]}], a[{wdtArr[68]}], a[{wdtArr[69]}], a[{wdtArr[70]}], a[{wdtArr[71]}], a[{wdtArr[72]}], a[{wdtArr[73]}], 
              a[{wdtArr[74]}], a[{wdtArr[75]}], a[{wdtArr[76]}], a[{wdtArr[77]}], str2Num(a[{wdtArr[78]}]), str2Num(a[{wdtArr[79]}]), a[{wdtArr[80]}], a[{wdtArr[81]}], a[{wdtArr[82]}], a[{wdtArr[83]}],
    arr[0], arr[1], arr[2]))";

            return res;

        }


        public string GetTradeItemPara()
        {
            // 旺店通列字段
            string wdtText = "['', '订单编号', '店铺', '订单来源', '仓库', '子单原始单号', '订单状态', '订单类型', '货到付款', '订单退款状态', '交易时间', '付款时间', '发货时间', '客户网名', '收件人', '收货地区', '收货地址', '收件人手机', '分销商', '分销商编号', '分销原始单号', '子单原始子单号', '来源组合装编号', '客户编号', '收件人电话', '邮编', '区域', '物流公司', '物流单号', '买家留言', '客服备注', '打印备注', '备注', '客服标旗', '订单支付金额', '邮费', '其它费用', '订单总优惠', '应收金额', '款到发货金额', '货到付款金额', '订单预估成本', '邮资成本', '预估重量', '需要发票', '发票抬头', '发票内容', '业务员', '审核人', '财审人', '商家编码', '货品编号', '货品名称', '规格名称', '货品简称', '基本单位', '平台货品名称', '平台规格名称', '平台货品ID', '平台规格ID', '下单数量', '标价', '货品总优惠', '成交价', '分摊后价格', '折扣', '实发数量', '分摊后总价', '退款前支付金额', '分摊邮费', '单品支付金额', '佣金', '拆自组合装', '估重', '赠品方式', '证件号码', '标记名称', '激活时间', '派送时间']";

            //sql 插入字段
            var sqlFields = new string[]
            {
                "trade_no",
"shop_name",
"trade_from",
"warehouse",
"src_tids",
"trade_status",
"trade_type",
"cash_on_delivery",
"refund_status",
"trade_time",
"pay_time",
"buyer_nick",
"receiver_name",
"receiver_district",
"receiver_address",
"receiver_mobile",
"fenxiao_nick",
"src_tid",
"suit_no",
"receiver_phone2",
"receiver_zip",
"receiver_ring",
"logistics_name",
"logistics_no",
"buyer_message",
"cs_remark",
"print_remark",
"remark",
"cs_flag",
"paid",
"post_amount",
"other_amount",
"order_amount",
"receivable",
"dap_amount",
"cod_amount",
"trade_calc_cost",
"post_cost",
"calc_weight",
"invoicing_required",
"invoice_title",
"invoice_content",
"salesman",
"checker",
"fchecker",
"spec_no",
"goods_no",
"goods_name",
"spec_name",
"api_goods_name",
"api_spec_name",
"num",
"price",
"discount",
"sell_price",
"share_price",
"discount_rate",
"actual_num",
"share_amount",
"paid_before_return",
"share_post",
"single_goods_paid",
"commission",
"self_assembly",
"weight",
"gift_type",
"id_card",
"flag_name",
"activation_time",
"platform_goods_id",
"platform_spec_id",
"base_unit",
"fenxiao_tid",
"goods_shot_name",
"customer_no",
"fenxiao_no",
"to_deliver_time",            };

            // 顺序
            var order = new Dictionary<string, string>
            {
               {"trade_no", "订单编号" },
{"shop_name", "店铺" },
{"trade_from", "订单来源" },
{"warehouse", "仓库" },
{"src_tids", "子单原始单号" },
{"trade_status", "订单状态" },
{"trade_type", "订单类型" },
{"cash_on_delivery", "货到付款" },
{"refund_status", "订单退款状态" },
{"trade_time", "交易时间" },
{"pay_time", "付款时间" },
{"buyer_nick", "客户网名" },
{"receiver_name", "收件人" },
{"receiver_district", "收货地区" },
{"receiver_address", "收货地址" },
{"receiver_mobile", "收件人手机" },
{"fenxiao_nick", "分销商" },
{"src_tid", "子单原始子单号" },
{"suit_no", "来源组合装编号" },
{"receiver_phone2", "收件人电话" },
{"receiver_zip", "邮编" },
{"receiver_ring", "区域" },
{"logistics_name", "物流公司" },
{"logistics_no", "物流单号" },
{"buyer_message", "买家留言" },
{"cs_remark", "客服备注" },
{"print_remark", "打印备注" },
{"remark", "备注" },
{"cs_flag", "客服标旗" },
{"paid", "订单支付金额" },
{"post_amount", "邮费" },
{"other_amount", "其它费用" },
{"order_amount", "订单总优惠" },
{"receivable", "应收金额" },
{"dap_amount", "款到发货金额" },
{"cod_amount", "货到付款金额" },
{"trade_calc_cost", "订单预估成本" },
{"post_cost", "邮资成本" },
{"calc_weight", "预估重量" },
{"invoicing_required", "需要发票" },
{"invoice_title", "发票抬头" },
{"invoice_content", "发票内容" },
{"salesman", "业务员" },
{"checker", "审核人" },
{"fchecker", "财审人" },
{"spec_no", "商家编码" },
{"goods_no", "货品编号" },
{"goods_name", "货品名称" },
{"spec_name", "规格名称" },
{"api_goods_name", "平台货品名称" },
{"api_spec_name", "平台规格名称" },
{"num", "下单数量" },
{"price", "标价" },
{"discount", "货品总优惠" },
{"sell_price", "成交价" },
{"share_price", "分摊后价格" },
{"discount_rate", "折扣" },
{"actual_num", "实发数量" },
{"share_amount", "分摊后总价" },
{"paid_before_return", "退款前支付金额" },
{"share_post", "分摊邮费" },
{"single_goods_paid", "单品支付金额" },
{"commission", "佣金" },
{"self_assembly", "拆自组合装" },
{"weight", "估重" },
{"gift_type", "赠品方式" },
{"id_card", "证件号码" },
{"flag_name", "标记名称" },
{"activation_time", "激活时间" },
{"platform_goods_id", "平台货品ID" },
{"platform_spec_id", "平台规格ID" },
{"base_unit", "基本单位" },
{"fenxiao_tid", "分销商编号" },
{"goods_shot_name", "货品简称" },
{"customer_no", "客户编号" },
{"fenxiao_no", "分销原始单号" },
{"to_deliver_time", "派送时间" },

            };

            int i = 1;


            var wdtArr2 = wdtText.Trim('[', ']')
            .Replace("'", string.Empty)
            .Replace(" ", string.Empty)
            .Split(',')
            .Select(a =>
            {
                var find = order.Where(c => c.Value == a).FirstOrDefault();

                if (string.IsNullOrEmpty(find.Key) == false)
                {
                    var ind = Array.IndexOf(sqlFields, find.Key);
                    //第一个元素舍弃， 这里减1
                    return new { ind = ind, val = i++, name = a, field = find.Key };  //ind 是sql参数的顺序, val是旺店通的
                }
                else
                {
                    return new { ind = -1, val = -1, name = string.Empty, field = string.Empty };
                }
            })
            .Where(a => a.ind >= 0)
            .OrderBy(a => a.ind)
            .ToList();

            foreach (var item in wdtArr2)
            {
                Console.WriteLine(item);
            }
            var nof = sqlFields.Where(a => wdtArr2.Any(c => c.field == a) == false)
                .ToList();
            foreach (var item in nof)
            {
                Console.WriteLine("找不到的: "+ item);
            }


            var wdtArr = wdtArr2.Select(a => a.val)
            .ToArray();


            var res = $@" data.append((a[{wdtArr[0]}] , a[{wdtArr[1]}] , a[{wdtArr[2]}] , a[{wdtArr[3]}] , a[{wdtArr[4]}] , a[{wdtArr[5]}] , a[{wdtArr[6]}] , a[{wdtArr[7]}] , a[{wdtArr[8]}] , a[{wdtArr[9]}] , a[{wdtArr[10]}] , a[{wdtArr[11]}] , a[{wdtArr[12]}] , a[{wdtArr[13]}] , a[{wdtArr[14]}] , 
a[{wdtArr[15]}] , a[{wdtArr[16]}] , a[{wdtArr[17]}] , a[{wdtArr[18]}] , a[{wdtArr[19]}] , a[{wdtArr[20]}] , a[{wdtArr[21]}] , a[{wdtArr[22]}] , a[{wdtArr[23]}] , a[{wdtArr[24]}] , a[{wdtArr[25]}] , a[{wdtArr[26]}] , a[{wdtArr[27]}] , a[{wdtArr[28]}] , 
str2Num(a[{wdtArr[29]}]) , str2Num(a[{wdtArr[30]}]) , str2Num(a[{wdtArr[31]}]) , str2Num(a[{wdtArr[32]}]) , str2Num(a[{wdtArr[33]}]) , str2Num(a[{wdtArr[34]}]) , 
str2Num(a[{wdtArr[35]}]) , str2Num(a[{wdtArr[36]}]) , str2Num(a[{wdtArr[37]}]) , str2Num(a[{wdtArr[38]}]) , a[{wdtArr[39]}] , a[{wdtArr[40]}] , a[{wdtArr[41]}] , a[{wdtArr[42]}] , 
a[{wdtArr[43]}] , a[{wdtArr[44]}] , a[{wdtArr[45]}] , a[{wdtArr[46]}] , a[{wdtArr[47]}] , a[{wdtArr[48]}] , a[{wdtArr[49]}] , a[{wdtArr[50]}] , str2Num(a[{wdtArr[51]}]) , str2Num(a[{wdtArr[52]}]) , str2Num(a[{wdtArr[53]}]) , 
str2Num(a[{wdtArr[54]}]) , str2Num(a[{wdtArr[55]}]) , str2Num(a[{wdtArr[56]}]) , str2Num(a[{wdtArr[57]}]) , str2Num(a[{wdtArr[58]}]) , str2Num(a[{wdtArr[59]}]) , str2Num(a[{wdtArr[60]}]) , 
str2Num(a[{wdtArr[61]}]) , str2Num(a[{wdtArr[62]}]) , a[{wdtArr[63]}] , str2Num(a[{wdtArr[64]}]) , a[{wdtArr[65]}] , a[{wdtArr[66]}] , a[{wdtArr[67]}] , a[{wdtArr[68]}] , a[{wdtArr[69]}] , a[{wdtArr[70]}] , 
a[{wdtArr[71]}] , a[{wdtArr[72]}] , a[{wdtArr[73]}] , a[{wdtArr[74]}] , a[{wdtArr[75]}] , a[{wdtArr[76]}]))";

            return res;

        }




    }
}
